{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "94f46ef4",
   "metadata": {},
   "source": [
    "* 学习人：郭善霖\n",
    "* 时间：2021-11-29\n",
    "* week14"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f93a8d3",
   "metadata": {},
   "source": [
    "#  本周学习目标\n",
    "\n",
    "> 1. 函数的定义\n",
    "> 2. 函数的使用\n",
    "> 3. 函数的参数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3fa50632",
   "metadata": {},
   "source": [
    "## 函数的定义\n",
    "\n",
    "\n",
    "> 定义一个函数的一般步骤"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4bdd2730",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 10:\n",
    "    print(a, end=' ')\n",
    "    a, b = b, a+b\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "4df49512",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 50:\n",
    "    print(a, end=' ')\n",
    "    a, b = b, a+b\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "37f692b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 \n"
     ]
    }
   ],
   "source": [
    "a, b = 0, 1\n",
    "while a < 100:\n",
    "    print(a, end=' ')\n",
    "    a, b = b, a+b\n",
    "print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "e9f67228",
   "metadata": {},
   "outputs": [],
   "source": [
    "# .....我想重复使用某一个功能（避免重复造轮子）\n",
    "# .....使用的人可以不懂代码，只要会调用函数即可 function_name()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51f449f8",
   "metadata": {},
   "source": [
    "> 1. 定义函数的一般方法：\n",
    "...\n",
    "\n",
    "def funcation_name() :  # 1. function_name 遵循变量定义的规则 区别数学函数==> f(x) python的函数可以自定义名称\n",
    "    \"\"\"函数的注释\"\"\"   # 2. 函数的注释可以和使用者交流\n",
    "    函数体          #  3.函数体要和定义函数保持一定的tab缩进  \n",
    "..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da7eeece",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "14b5bfed",
   "metadata": {},
   "outputs": [],
   "source": [
    "def fib(n):   # Print a Fibonacci series up to n\n",
    "    \"\"\"Print a Fibonacci series up to n.\"\"\"\n",
    "    a, b = 0, 1\n",
    "    while a < n:\n",
    "        print(a, end=' ')\n",
    "        a, b = b, a+b\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8648f8e5",
   "metadata": {},
   "source": [
    "## 函数的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "490ea016",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 \n"
     ]
    }
   ],
   "source": [
    "fib(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "52d8457d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 \n"
     ]
    }
   ],
   "source": [
    "fib(50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "5fc1dc18",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 1 1 2 3 5 8 13 21 34 55 89 \n"
     ]
    }
   ],
   "source": [
    "fib(100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "f9b0f27f",
   "metadata": {},
   "outputs": [],
   "source": [
    "1. 请大家写一个函数 实现 hello world\n",
    "2. 请大家写一个函数 实现 hello username "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "860dac18",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello():\n",
    "    \"\"\"打印你好，世界\"\"\"\n",
    "    print(\"hello,world\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "f7c4221b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello,world\n"
     ]
    }
   ],
   "source": [
    "hello()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "05053622",
   "metadata": {},
   "outputs": [],
   "source": [
    "def hello_user(username):\n",
    "    \"\"\"打印，你好，xxx同学\"\"\"\n",
    "    print(\"你好，\" + username.title()+\"同学\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "f5e4cca1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你好，Shanlin同学\n"
     ]
    }
   ],
   "source": [
    "hello_user(\"shanlin\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3743b0f",
   "metadata": {},
   "source": [
    "* 1. 思考： 函数是否必须有 参数？\n",
    "* 注意：\n",
    "> 1. 在第一行定义函数中，不需要任何信息就能完成工作，因此括号是空的（即便如此，括号必不可少！）\n",
    "> 2. 要调用函数，可以依次指定函数名以及括号括起的必要信息。在第五行代码中，因为此函数hello()括号中不要任何信息，只需要输入hello（）即可。和预期效果一样，打印hello，world！"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b98cb647",
   "metadata": {},
   "source": [
    "## 3.函数的参数\n",
    "\n",
    "> 1. 形式参数： 定义函数时，写在 function_name 括号中的参数\n",
    ">> 1. 在定义函数时，形式参数不占用空间\n",
    "> 2. 实际参数： 调用函数时，写在 function_name 括号中的参数\n",
    ">> 2. 在使用函数时，形式参数 被 分配空间， 变为实际参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b4e74fc7",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):   # animal_type,pet_name 是形式参数\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI have a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7b896f2b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a dog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"dog\",\"豆豆\")  # \"dog\",\"豆豆\" 实际参数 实参"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "669cf834",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I have a 豆豆.\n",
      "My豆豆's name is Dog.\n"
     ]
    }
   ],
   "source": [
    "describe_pet(\"豆豆\",\"dog\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "be3b9cbf",
   "metadata": {},
   "source": [
    "* 当函数具有多个参数时\n",
    "\n",
    "> 1. 位置参数\n",
    ">> 1. 形式参数有先后顺序。函数调用过程中应考虑参数的先后顺序，避免出现顺序错误的笑话\n",
    ">> 2. 要考虑实际调用函数过程中要考虑形式参数的个数\n",
    "> 2. 关键字参数\n",
    "> 3. 默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "29ddba0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "def describe_pet(animal_type,pet_name):   # animal_type,pet_name 是形式参数\n",
    "    \"\"\"显示宠物信息\"\"\"\n",
    "    print(\"\\nI havet a \" + animal_type + \".\")\n",
    "    print(\"My\" + animal_type + \"'s name is \"+pet_name.title() + \".\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "8b6f7d4a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I havet a dog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "# 在使用 describe_pet() 函数时 需要考虑 参数的位置， 从而进行参数数据的输入（实参的调用）\n",
    "\n",
    "describe_pet(\"dog\",\"豆豆\") # 要根据 形式参数的位置先后顺序  先写animal_type 再写 pet_name"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "9186fb09",
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "describe_pet() missing 2 required positional arguments: 'animal_type' and 'pet_name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-43-f95e5f55e3d1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdescribe_pet\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m: describe_pet() missing 2 required positional arguments: 'animal_type' and 'pet_name'"
     ]
    }
   ],
   "source": [
    "describe_pet()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "9e28ad5c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "I havet a dog.\n",
      "Mydog's name is 豆豆.\n"
     ]
    }
   ],
   "source": [
    "# 关键词参数\n",
    "\n",
    "describe_pet(animal_type=\"dog\",pet_name=\"豆豆\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf8ce814",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e00d6a3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
